package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.CTGDM;
import net.sf.json.JSONObject;
import dao.CTGDD;

@WebServlet("/CTGDServlet")
public class CTGDServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CTGDD dao;
       
    /**
     * @function constructor MonHocServlet
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public CTGDServlet() throws Exception {
        super();
        this.dao = new CTGDD();
    }
    
    /**
	 * Destruction of the servlet.
	 */
	public void destroy() {
		super.destroy();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
        response.setHeader("Cache-control", "no-cache, no-store");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "-1");
		
		String flag = request.getParameter("flag");
		try {
			if (flag == null || "".equals(flag.trim())) {
				showCTGD(request, response);
			}else if ("add".equals(flag.trim())) {
				String ten_van_tat = request.getParameter("ten_van_tat");
				String ten_day_du = request.getParameter("ten_day_du");
				String nam_bd_tuyen_sinh = request.getParameter("nam_bd_tuyen_sinh");
				String nam_kt_tuyen_sinh = request.getParameter("nam_kt_tuyen_sinh");
				String ts_tc = request.getParameter("ts_tc");
				String id_ctdt = request.getParameter("id_ctdt");
				System.out.print(ten_van_tat);
				addCTGD(ten_van_tat,ten_day_du,Integer.parseInt(nam_bd_tuyen_sinh),Integer.parseInt(nam_kt_tuyen_sinh),Integer.parseInt(ts_tc), Integer.parseInt(id_ctdt),request,response);
			}else if ("edit".equals(flag.trim())) {
				String id = request.getParameter("id_ctgd");
				String ten_van_tat = request.getParameter("ten_van_tat");
				String ten_day_du = request.getParameter("ten_day_du");
				String nam_bd_tuyen_sinh = request.getParameter("nam_bd_tuyen_sinh");
				String nam_kt_tuyen_sinh = request.getParameter("nam_kt_tuyen_sinh");
				String ts_tc = request.getParameter("ts_tc");
				String id_ctdt = request.getParameter("id_ctdt");
				editCTGD(Integer.parseInt(id), ten_van_tat,ten_day_du,Integer.parseInt(nam_bd_tuyen_sinh),Integer.parseInt(nam_kt_tuyen_sinh),Integer.parseInt(ts_tc),Integer.parseInt(id_ctdt), request, response);
			}else if ("remove".equals(flag.trim())) {
				String[] id = request.getParameter("id").split(",");
				for(int i=0; i<id.length; i++){
					deleteCTGD(Integer.parseInt(id[i]), request, response);
				}			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * @function show MonHoc
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void showCTGD(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String pageCount = request.getParameter("page");
		String rowsCount = request.getParameter("rows");
		String search=request.getParameter("searchTen")==null?"":request.getParameter("searchTen");
		
		int page = (pageCount == null || "".equals(pageCount)) ? 1 : Integer.valueOf(pageCount);
		int rows = (rowsCount == null || "".equals(rowsCount)) ? 10 : Integer.valueOf(rowsCount);
		
		int offSet = (page - 1) * rows;
		int allCount = dao.searchCTGD(search).size();
		
		returnMap.put("total", allCount);
		returnMap.put("rows", dao.getAlls(search, offSet, rows));
		
		String returnString = JSONObject.fromObject(returnMap).toString();
		PrintWriter out = response.getWriter();
		out.println(returnString);
		out.flush();
		out.close();
	}
	
	/**
	 * @function add MonHoc
	 * @param ten_van_tat
	 * @param ten_day_du
	 * @param nam_bd_tuyen_sinh
	 * @param nam_kt_tuyen_sinh
	 * @param ts_tc
	 * @param id_ctdt
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void addCTGD(String ten_van_tat, String ten_day_du, int nam_bd_tuyen_sinh, int nam_kt_tuyen_sinh, int ts_tc, int id_ctdt, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		CTGDM ctgd = new CTGDM();
		ctgd.setTen_van_tat(ten_van_tat);
		ctgd.setTen_day_du(ten_day_du);
		ctgd.setNam_bd_tuyen_sinh(nam_bd_tuyen_sinh);
		ctgd.setNam_kt_tuyen_sinh(nam_kt_tuyen_sinh);
		ctgd.setTs_tc(ts_tc);
		ctgd.setId_ctdt(id_ctdt);
		if(dao.Insert(ctgd)==true){
			map.put("success", "true");
			map.put("msg", "Thêm thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi thêm dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
	
	/**
	 * @function edit MonHoc
	 * @param id_ctgd
	 * @param ten_van_tat
	 * @param ten_day_du
	 * @param nam_bd_tuyen_sinh
	 * @param nam_kt_tuyen_sinh
	 * @param ts_tc
	 * @param id_ctdt
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void editCTGD(int id_ctgd, String ten_van_tat, String ten_day_du, int nam_bd_tuyen_sinh, int nam_kt_tuyen_sinh, int ts_tc, int id_ctdt, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		CTGDM ctgd = new CTGDM();
		ctgd.setId_ctgd(id_ctgd);
		ctgd.setTen_van_tat(ten_van_tat);
		ctgd.setTen_day_du(ten_day_du);
		ctgd.setNam_bd_tuyen_sinh(nam_bd_tuyen_sinh);
		ctgd.setNam_kt_tuyen_sinh(nam_kt_tuyen_sinh);
		ctgd.setTs_tc(ts_tc);
		ctgd.setId_ctdt(id_ctdt);
		if(dao.Update(ctgd)==true){
			map.put("success", "true");
			map.put("msg", "Sửa thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi sửa dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
	
	/**
	 * @function delete MonHoc
	 * @param id_mon_hoc
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void deleteCTGD(int id_ctgd, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if(dao.Delete(id_ctgd)==true){
			map.put("success", "true");
			map.put("msg", "Xóa thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi xóa dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
}
